using System;
using System.Collections.Generic;
using System.Text;

using DotNetWarcraft.WoWGuildRoster;

namespace DotNetWarcraft.WoWGuildRoster.Business {
  public abstract class LoadBase {
    private DataLoad _dataLoad;
    protected String _namespace = "DotNetWarcraft.WoWGuildRoster.Business";

    public virtual int Version_Major {
      get {
        return 0;
      }
    }
    public virtual int Version_Minor {
      get {
        return 0;
      }
    }
    public virtual int Version_Build {
      get {
        return 0;
      }
    }
    public String Version {
      get {
        return this.Version_Major.ToString() + "." + this.Version_Minor.ToString() + this.Version_Build.ToString();
      }
    }

    protected int Status_Adds {
      get {
        return this.LoadEntry.LoadCount_Adds;
      }
      set {
        this.LoadEntry.LoadCount_Adds = value;
      }
    }
    protected int Status_Errors {
      get {
        return this.LoadEntry.LoadCount_Errors;
      }
      set {
        this.LoadEntry.LoadCount_Errors = value;
      }
    }
    protected int Status_Skips {
      get {
        return this.LoadEntry.LoadCount_Skips;
      }
      set {
        this.LoadEntry.LoadCount_Skips = value;
      }
    }
    protected int Status_Updates {
      get {
        return this.LoadEntry.LoadCount_Updates;
      }
      set {
        this.LoadEntry.LoadCount_Updates = value;
      }
    }

    public DataLoad LoadEntry {
      get {
        if (_dataLoad == null) {
          _dataLoad = new DataLoad();
          _dataLoad.LoadTime_Started = DateTime.UtcNow;
          _dataLoad.LoadStatus_Enum = WGR_LoadStatus.LoadInProgress;
        }
        return _dataLoad;
      }
      set {
        _dataLoad = value;

        if (value.LoadTime_Started == null || value.LoadTime_Started == DateTime.MinValue || value.LoadTime_Started == DateTime.MaxValue) {
          _dataLoad.LoadTime_Started = DateTime.UtcNow;
        }

        if (value.LoadStatus == null || value.LoadStatus < 0) {
          _dataLoad.LoadStatus_Enum = WGR_LoadStatus.LoadInProgress;
        }

      }
    }

    public LoadBase() {
      this.UpdateLoadEntry();
    }
    public LoadBase(DataLoad existingDataLoad) {
      this.LoadEntry = existingDataLoad;
      this.UpdateLoadEntry();
    }

    public abstract void Execute();

    public void UpdateLoadEntry() {
      if (this.LoadEntry.Load_ID > 0) {
        GuildRosterController.UpdateDataLoad(this.LoadEntry);
      } else {
        this.LoadEntry.Load_ID = GuildRosterController.AddDataLoad(this.LoadEntry);
      }
    }

    }

  public abstract class LoadRoster : LoadBase {
    protected GuildRoster Roster {
      get;
      set;
    }
    public int RosterID {
      get {
        return this.Roster.Roster_ID;
      }
      set {
        this.Roster.Roster_ID = value;
      }
    }
    public String GuildName {
      get {
        return this.Roster.Guild_Name;
      }
      set {
        this.Roster.Guild_Name = value;
      }
    }
    public String ServerName {
      get {
        return this.Roster.Guild_Server;
      }
      set {
        this.Roster.Guild_Server = value;
      }
    }
    public WarcraftRegions Region {
      get {
        return this.Roster.Guild_Region_Enum;
      }
      set {
        this.Roster.Guild_Region_Enum = value;
      }
    }

    public LoadRoster() : base() {
      this.Roster = new GuildRoster();
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.RosterLoad;
    }
    public LoadRoster(int rosterID) : base() {
      this.Roster = GuildRosterController.GetRoster_DB(rosterID);
      if (this.Roster == null || this.RosterID < 0) {
        (new Business.Exceptions.GuildRosterError(new ApplicationException("LoadRoster(" + rosterID.ToString() + ") - No roster was found with this ID"), -1, rosterID, _namespace + ".LoadRoster.Execute(" + rosterID.ToString() + ")")).WriteExceptionToDB();
        return;
      }
      this.LoadEntry.Roster_ID = this.RosterID;
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.RosterLoad;

    }
    ~LoadRoster() {
      this.LoadEntry.LoadTime_Ended = DateTime.UtcNow;
      this.LoadEntry.LoadStatus_Enum = WGR_LoadStatus.LoadedSuccessfully;

      if (this.LoadEntry.Load_ID > 0) {
        GuildRosterController.UpdateDataLoad(this.LoadEntry);
      } else {
        GuildRosterController.AddDataLoad(this.LoadEntry);
      }
    }
  }

  public abstract class LoadCharEntry : LoadBase {
    protected GuildEntry Entry {
      get;
      set;
    }
    public int CharID {
      get {
        return this.Entry.Char_ID;
      }
      set {
        this.Entry.Char_ID = value;
      }
    }
    public int RosterID {
      get;
      set;
    }
    public String CharName {
      get {
        return this.Entry.Char_Name;
      }
      set {
        this.Entry.Char_Name = value;
      }
    }
    public String ServerName {
      get {
        return this.Entry.Char_Server;
      }
      set {
        this.Entry.Char_Server = value;
      }
    }
    public WarcraftRegions Region {
      get {
        return this.Entry.Char_Region_Enum;
      }
      set {
        this.Entry.Char_Region_Enum = value;
      }
    }

    public LoadCharEntry() : base() {
      this.Entry = new GuildEntry();
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.CharEntryLoad;
    }
    public LoadCharEntry(DataLoad existingDataLoad) : base(existingDataLoad) {
      this.Entry = new GuildEntry();
    }
    public LoadCharEntry(int charEntryID)
      : base() {
      this.Entry = GuildRosterController.GetCharEntry_DB(charEntryID);
      if (this.Entry == null || this.CharID < 0) {
        (new Business.Exceptions.GuildRosterError(new ApplicationException("LoadCharEntry(" + charEntryID.ToString() + ") - No character was found with this ID"), -1, this.RosterID, _namespace + ".LoadCharEntry(" + this.RosterID.ToString() + ")")).WriteExceptionToDB();
        return;
      }
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.CharEntryLoad;

    }
    public LoadCharEntry(int charEntryID, DataLoad existingDataLoad)
      : base(existingDataLoad) {
      this.Entry = GuildRosterController.GetCharEntry_DB(charEntryID);
      if (this.Entry == null || this.CharID < 0) {
        (new Business.Exceptions.GuildRosterError(new ApplicationException("LoadCharEntry(" + charEntryID.ToString() + ") - No character was found with this ID"), -1, this.RosterID, _namespace + ".LoadCharEntry(" + this.RosterID.ToString() + ")")).WriteExceptionToDB();
        return;
      }
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.CharEntryLoad;

    }
    public LoadCharEntry(String charName, String serverName) : base() {
      this.Entry = GuildRosterController.GetCharEntry_DB(charName, serverName);
      if (this.Entry == null || this.CharID < 0) {
        (new Business.Exceptions.GuildRosterError(new ApplicationException("LoadCharEntry(" + charName + ", " + serverName + ") - No character was found with this name and server"), -1, -1, _namespace + ".LoadCharEntry(" + charName + ", " + serverName + ")")).WriteExceptionToDB();
        return;
      }
      this.LoadEntry.LoadType_Enum = WGR_LoadTypes.CharEntryLoad;
    }
    ~LoadCharEntry() {
      this.LoadEntry.LoadTime_Ended = DateTime.UtcNow;

      if (this.LoadEntry.Load_ID > 0) {
        GuildRosterController.UpdateDataLoad(this.LoadEntry);
      } else {
        GuildRosterController.AddDataLoad(this.LoadEntry);
      }
    }

  }

}
